package com.friend.chat.page.vm

import androidx.lifecycle.MutableLiveData
import com.friend.chat.base.BasicViewModel
import com.friend.chat.net.BaseResult
import com.friend.chat.page.bean.FriendBean
import com.friend.chat.page.bean.GroupBean
import com.friend.chat.page.bean.RoomBean

/**
 * @des
 * @date 2021/10/8
 * @author gorilla
 */
class MainViewModel: BasicViewModel() {

    val roomListResponse by lazy { MutableLiveData<BaseResult<List<RoomBean>>>() }

    fun getRoomList(userId:String) {
        netWorkJob(
            successResult = {
                val data = mRepository.getRoomList(userId)
                if (!data.isOk()) {
                    toast(data.message)
                } else {
                    roomListResponse.value = data
                }
            },
            errorResult = {
                toast("错误")
            }
        )
    }



    //通讯录
    val contactListResponse by lazy { MutableLiveData<BaseResult<List<FriendBean>>>() }

    fun getContactList(userId:String) {
        netWorkJob(
            successResult = {
                val data = mRepository.getFriendsList(userId)
                if (!data.isOk()) {
                    toast(data.message)
                } else {
                    contactListResponse.value = data
                }
            },
            errorResult = {
                toast("错误")
            }
        )
    }

    //通讯录 -群列表
    val groupListResponse by lazy { MutableLiveData<BaseResult<List<GroupBean>>>() }

    fun getGroupList(userId: String) {
        netWorkJob(
            successResult = {
                val data = mRepository.getGroupList(userId)
                if (!data.isOk()) {
                    toast(data.message)
                } else {
                    groupListResponse.value = data
                }
            },
            errorResult = {
                toast("错误")
            }
        )
    }

    //添加好友
    val addFriendResponse by lazy { MutableLiveData<BaseResult<Boolean>>() }

    fun addFriend(userId: String, friendId: String) {
        netWorkJob(
            successResult = {
                val data = mRepository.addFriend(userId, friendId)
                if (!data.isOk()) {
                    toast(data.message)
                } else {
                    addFriendResponse.value = data
                }
            },
            errorResult = {
                toast("错误")
            }
        )
    }
}